package cn.hsa.fqz.monitor.controller;

import cn.hsa.fqz.handler.base.BaseController;
import cn.hsa.fqz.monitor.controller.vo.*;
import cn.hsa.fqz.monitor.entity.*;
import cn.hsa.fqz.monitor.service.MonitorEntityService;
import cn.hsa.hsaf.core.framework.util.PageResult;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 指标监测配置控制类
 *
 * @author Administrator
 * @version 1.0
 * @date 2023-12-26 11:22
 */
@Slf4j
@Api(value = "指标监测配置", tags = "指标监测配置")
@RestController
@RequestMapping("/monitor/config")
@CrossOrigin(value = "*",maxAge = 3600)
public class TMonitorConfigController extends BaseController {

    @Autowired
    private MonitorEntityService monitorEntityService;

    @Value("${zmjk.ck.monitor.host}")
    private String ckHost;

    @Value("${zmjk.ck.monitor.passwd}")
    private String ckPasswd;

    @Value("${zmjk.ck.monitor.port}")
    private Integer ckPort;


    @ApiOperation(value = "新增业务类型", notes = "新增业务类型")
    @PostMapping("/buss")
    public WrapperResponse<Boolean> insertBuss(@RequestBody MonitorBussTypeVO vo){
        Boolean result = monitorEntityService.insertBuss(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "修改业务类型", notes = "修改业务类型")
    @PutMapping("/buss")
    public WrapperResponse<Boolean> updateBuss(@RequestBody MonitorBussTypeVO vo){
        Boolean result = monitorEntityService.updateBuss(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "删除业务类型", notes = "删除业务类型")
    @DeleteMapping("/buss")
    public WrapperResponse<Boolean> deleteBuss(@RequestParam("bussId")Integer bussId){
        Boolean result = monitorEntityService.deleteBuss(bussId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "分页查询业务类型", notes = "分页查询业务类型")
    @PostMapping("/page/buss")
    public WrapperResponse<PageResult<MonitorBussTypeVO>> selectPageBuss(@RequestBody TMonitorConfigQueryVO vo){
        PageResult<MonitorBussTypeVO> result = monitorEntityService.selectPageBuss(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "新增维度", notes = "新增维度")
    @PostMapping("/dimension")
    public WrapperResponse<Boolean> insertDimension(@RequestBody DimensionVO vo){
        Boolean result = monitorEntityService.insertDimension(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "修改维度", notes = "修改维度")
    @PutMapping("/dimension")
    public WrapperResponse<Boolean> updateDimension(@RequestBody DimensionVO vo){
        Boolean result = monitorEntityService.updateDimension(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "分页查询维度", notes = "分页查询维度")
    @PostMapping("/page/dimension")
    public WrapperResponse<PageResult<TMonitorDimension>> selectPageDimension(@RequestBody TMonitorConfigQueryVO vo){
        PageResult<TMonitorDimension> result = monitorEntityService.selectPageDimension(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "删除维度", notes = "删除维度")
    @DeleteMapping("/dimension")
    public WrapperResponse<Boolean> deleteDimension(@RequestParam("dimensionId")Integer dimensionId){
        Boolean result = monitorEntityService.deleteDimension(dimensionId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "新增动态sql配置", notes = "新增动态sql配置")
    @PostMapping("/entity/sql")
    public WrapperResponse<Boolean> insertExcuteSql(@RequestBody EntityExcuteSqlVO vo){
        Boolean result = monitorEntityService.insertExcuteSql(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "修改动态sql配置", notes = "修改动态sql配置")
    @PutMapping("/entity/sql")
    public WrapperResponse<Boolean> updateExcuteSql(@RequestBody EntityExcuteSqlVO vo){
        Boolean result = monitorEntityService.updateExcuteSql(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "分页查询动态sql配置", notes = "分页查询动态sql配置")
    @PostMapping("/page/entity/sql")
    public WrapperResponse<PageResult<EntityExcuteSqlVO>> selectPageExcuteSql(@RequestBody TMonitorConfigQueryVO vo){
        PageResult<EntityExcuteSqlVO> result = monitorEntityService.selectPageExcuteSql(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "删除动态sql配置", notes = "删除动态sql配置")
    @DeleteMapping("/entity/sql")
    public WrapperResponse<Boolean> deleteExcuteSql(@RequestParam("sqlId")Integer sqlId){
        Boolean result = monitorEntityService.deleteExcuteSql(sqlId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "获取单个动态sql配置以及属性", notes = "获取单个动态sql配置以及属性")
    @GetMapping("/entity/sql/properties")
    public WrapperResponse<List<TMonitorEntityExecuteSqlProperties>> selectExecuteSqlAndProperties(@RequestParam("sqlId")Integer sqlId){
        List<TMonitorEntityExecuteSqlProperties> result = monitorEntityService.selectExecuteSqlAndProperties(sqlId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "新增实体维度配置", notes = "新增实体维度配置")
    @PostMapping("/entity/dimension")
    public WrapperResponse<Boolean> insertEntityDimension(@RequestBody EntityDimensionVO vo){
        Boolean result = monitorEntityService.insertEntityDimension(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "修改实体维度配置", notes = "修改实体维度配置")
    @PutMapping("/entity/dimension")
    public WrapperResponse<Boolean> updateEntityDimension(@RequestBody EntityDimensionVO vo){
        Boolean result = monitorEntityService.updateEntityDimension(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "分页查询实体维度配置", notes = "分页查询实体维度配置")
    @PostMapping("/page/entity/dimension")
    public WrapperResponse<PageResult<EntityDimensionVO>> selectPageEntityDimension(@RequestBody TMonitorConfigQueryVO vo){
        PageResult<EntityDimensionVO> result = monitorEntityService.selectPageEntityDimension(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "删除维度", notes = "删除维度")
    @DeleteMapping("/entity/dimension")
    public WrapperResponse<Boolean> deleteEntityDimension(@RequestParam("entityDimensionId")Integer entityDimensionId){
        Boolean result = monitorEntityService.deleteEntityDimension(entityDimensionId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "获取维度信息", notes = "获取维度信息")
    @GetMapping("/entity/dimension")
    public WrapperResponse<List<TMonitorDimension>> selectDimension(@RequestParam("entityCode")String entityCode){
        List<TMonitorDimension> result = monitorEntityService.selectDimension(entityCode);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "新增实体和业务类型关联", notes = "新增实体和业务类型关联")
    @PostMapping("/entity")
    public WrapperResponse<Boolean> insertEntityAndProperties(@RequestBody TMonitorEntityVO vo){
        Boolean result = monitorEntityService.insertEntityAndProperties(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "修改实体和属性", notes = "修改实体和属性")
    @PutMapping("/entity")
    public WrapperResponse<Boolean> updateEntityAndProperties(@RequestBody TMonitorEntityVO vo){
        Boolean result = monitorEntityService.updateEntityAndProperties(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "获取所有实体信息", notes = "获取所有实体信息")
    @GetMapping("/all/entities")
    public WrapperResponse<List<TMonitorEntity>> getAllEntities(){
        List<TMonitorEntity> entityList = monitorEntityService.getAllEntity();
        return WrapperResponse.success(entityList);
    }

    @ApiOperation(value = "删除实体和属性", notes = "删除实体和属性")
    @DeleteMapping("/entity")
    public WrapperResponse<Boolean> deleteEntityAndProperties(@RequestParam("entityCode")String entityId,@RequestParam("bussCode")String bussCode){
        Boolean result = monitorEntityService.deleteEntityAndProperties(entityId,bussCode);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "单条删除实体属性", notes = "单条删除实体属性")
    @DeleteMapping("/entity/property")
    public WrapperResponse<Boolean> deleteEntityProperties(@RequestParam("entityPropId")Integer entityPropId){
        Boolean result = monitorEntityService.deleteEntityProperties(entityPropId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "获取单个实体和属性", notes = "获取单个实体和属性")
    @GetMapping("/entity")
    public WrapperResponse<TMonitorEntityVO> selectEntityAndProperties(@RequestParam("entityCode")String entityCode,@RequestParam("bussCode")String bussCode){
        TMonitorEntityVO result = monitorEntityService.selectEntityAndProperties(entityCode,bussCode);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "获取单个实体的属性", notes = "获取单个实体的属性")
    @GetMapping("/entity/properties")
    public WrapperResponse<List<TMonitorEntityProperties>> selectEntityProperties(@RequestParam("entityCode")String entityCode,@RequestParam("bussCode")String bussCode){
        List<TMonitorEntityProperties> result = monitorEntityService.selectEntityProperties(entityCode,bussCode);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "获取单个实体维度的属性", notes = "获取单个实体维度的属性")
    @GetMapping("/entity/dimension/properties")
    public WrapperResponse<List<TMonitorEntityDimensionProperties>> selectEntityDimensionProperties(@RequestParam("entityCode")String entityCode, @RequestParam("tableName")String tableName){
        List<TMonitorEntityDimensionProperties> result = monitorEntityService.selectEntityDimensionProperties(entityCode,tableName);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "分页查询实体和属性", notes = "分页查询实体和属性")
    @PostMapping("/page/entity")
    public WrapperResponse<PageResult<TMonitorEntityVO>> selectPageEntityAndProperties(@RequestBody TMonitorConfigQueryVO vo){
        PageResult<TMonitorEntityVO> result = monitorEntityService.selectPageEntityAndProperties(vo);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "同步实体属性", notes = "同步实体属性")
    @PostMapping("/sync/entity/properties")
    public WrapperResponse<Boolean> syncMonitoryEntityProperties(@RequestParam("entityId") Integer entityId,@RequestParam("bussCode")String bussType){
        Boolean result = monitorEntityService.addSyncMonitoryEntityProperties(ckHost,ckPort,ckPasswd,entityId,bussType);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "同步实体维度属性", notes = "同步实体维度属性")
    @PostMapping("/sync/entity/dimension/properties")
    public WrapperResponse<Boolean> syncMonitoryEntityDimensionProperties(@RequestParam("entityDimensionId") Integer entityDimensionId){
        Boolean result = monitorEntityService.addSyncMonitoryEntityDimensionProperties(ckHost,ckPort,ckPasswd,entityDimensionId);
        return WrapperResponse.success(result);
    }

    @ApiOperation(value = "同步配置sql属性", notes = "同步配置sql属性")
    @PostMapping("/sync/entity/exec/sql/properties")
    public WrapperResponse<Boolean> syncMonitoryEntityExecSqlProperties(@RequestParam("entityExecSqlId") Integer entityExecSqlId){
        Boolean result = monitorEntityService.addSyncMonitoryEntityExecSqlProperties(ckHost,ckPort,ckPasswd,entityExecSqlId);
        return WrapperResponse.success(result);
    }
}
